#include <iostream>
#include <grpc++/grpc++.h>
#include "user_service.grpc.pb.h"
#include "user_service.pb.h"


        /**
         * 
            bool AddUser(int user_id, const std::string &name,int age);
            
         * 
         * 
         */

void AddUser(std::unique_ptr<user_service::UserService::Stub>& stub)
{
    std::cout<<"输入用户ID,姓名,年龄:";
    int user_id;
    std::string name;
    int age;
    std::cin>>user_id>>name>>age;
    user_service::CreateUserRequest request;
    request.set_id(user_id);
    request.set_name(name);
    request.set_age(age);
    user_service::CreateUserResponse response;
    grpc::ClientContext context;
    grpc::Status status = stub->CreateUser(&context,request,&response);
    if (!status.ok())
    {
        std::cout << "error: "<< status.error_message() << std::endl;
        return;
    }
    
    if (response.success())
    {
        std::cout << "AddUser success" << std::endl;
    }
    else
    {
        std::cout << "AddUser failed: " << status.error_message() << std::endl;
    }
}

/*
    bool GetUser(int user_id,user_service::UserInfo *user_info);

*/
void GetUser(std::unique_ptr<user_service::UserService::Stub>&stub)
{
        std::cout<<"输入用户ID:";
        int user_id;
        std::cin>>user_id;
        user_service::GetUserRequest request;
        request.set_id(user_id);
        user_service::GetUserResponse response;

        grpc::ClientContext context;
        grpc::Status status= stub->GetUser(&context,request,&response);
        if (!status.ok())
        {
            std::cout << "error: "<< status.error_message() << std::endl;
            return;
        }
        if (response.success())
        {
            //输出用户信息
            std::cout<<"用户ID: "<<response.user_info().id()<<\
                " 姓名: "<<response.user_info().name()<<\
                " 年龄: "<<response.user_info().age()<<std::endl;
        }
        else
        {
            std::cout << "GetUser failed: " << status.error_message() << std::endl;
        }
}

/*
            bool UpdateUser(int user_id, std::string name,int age);
            
*/
void UpdateUser(std::unique_ptr<user_service::UserService::Stub>&stub)
{
    std::cout<<"输入用户ID,姓名,年龄:";
    int user_id;
    std::string name;
    int age;
    std::cin>>user_id>>name>>age;
    user_service::UserInfo user;
    user.set_id(user_id);
    user.set_name(name);
    user.set_age(age);
    user_service::UpdateUserResponse response;
    
    grpc::ClientContext context;
    grpc::Status status= stub->UpdateUser(&context,user,&response);
    if (!status.ok())
    {
        std::cout << "error: "<< status.error_message() << std::endl;
        return;
    }
    if (response.success())
    {
        std::cout << "UpdateUser success" << std::endl;
    }
    else
    {
        std::cout << "UpdateUser failed: " << status.error_message() << std::endl;
    }

}

/*
    bool DeleteUser(int user_id);
*/
void DeleteUser(std::unique_ptr<user_service::UserService::Stub>&stub)
{
        std::cout<<"输入用户ID:";
        int user_id;
        std::cin>>user_id;
        user_service::DeleteUserRequest request;
        request.set_id(user_id);
        user_service::DeleteUserResponse response;
        
        grpc::ClientContext context;
        grpc::Status status = stub->DeleteUser(&context,request,&response);
        if (!status.ok())
        {
            std::cout << "error: "<< status.error_message() << std::endl;
            return;
        }
        if (response.success())
        {
            std::cout << "DeleteUser success" << std::endl;
        }
        else
        {
            std::cout << "DeleteUser failed: " << status.error_message() << std::endl;
        }
}

int main()
{
    std::unique_ptr<user_service::UserService::Stub> stub = user_service::UserService::NewStub(grpc::CreateChannel("0.0.0.0:50051", grpc::InsecureChannelCredentials()));
    int choice;
    while(1)
    {

        std::cout<<">>请输入选择:\n";
        std::cout<<"1. 添加用户 输入用户ID,姓名,年龄\n";
        std::cout<<"2. 查询用户 输入用户ID\n";
        std::cout<<"3. 更新用户 输入用户ID,姓名,年龄\n";
        std::cout<<"4. 删除用户 输入用户ID\n";
        std::cout<<"其他 退出\n";



        std::cin>>choice;
        switch (choice)
        {
        case 1:
            AddUser(stub);
            break;
        case 2:
            GetUser(stub);
            break;
        case 3:
            UpdateUser(stub);
            break;
        case 4:
            DeleteUser(stub);
            break;
        
        default:
            std::cout<<"退出\n";
            break;
        }
    }

    return 0;
}

